#include <iostream> 
#include <vector> 
#include <string> 

using namespace std;

class IVisitor;
class Student
{
public:
	Student(string nameStr, int money): m_sName(nameStr), m_nMoney(money){}
	virtual ~Student(){}
	const string& GetName() const
	{
		return m_sName;
	}
	virtual void Accept(IVisitor *pVisitor) = 0;
	void Pay(int nMoneyToPay)
	{
		m_nMoney -= nMoneyToPay;
	}
private:
	string m_sName;
	int m_nMoney;
};

class BoyStudent;
class GirlStudent;
class IVisitor
{
public:
	virtual ~IVisitor(){}
	virtual void Visit(BoyStudent *pBoyStudent) = 0;
	virtual void Visit(GirlStudent *pGirlStudent) = 0;
};

class BoyStudent : public Student
{
public:
	BoyStudent(string nameStr, int nMoney) : Student(nameStr, nMoney){}
	virtual void Accept(IVisitor *pVisitor)
	{
		pVisitor->Visit(this);
	}
};

class GirlStudent : public Student
{
public:
	GirlStudent(string nameStr, int nMoney) : Student(nameStr, nMoney){}
	virtual void Accept(IVisitor *pVisitor)
	{
		pVisitor->Visit(this);
	}
};

class ChargeMoney : public IVisitor
{
public:
	ChargeMoney(int nSum = 0) : m_nSum(nSum){}
	virtual void Visit(BoyStudent *pBoyStudent)
	{
		int nMoneyToPay = 2*PAY;
		pBoyStudent->Pay(nMoneyToPay);
		m_nSum += nMoneyToPay;
		cout<<pBoyStudent->GetName()<<" is charged "<<nMoneyToPay<<endl;
	}
	virtual void Visit(GirlStudent *pGirlStudent)
	{
		int nMoneyToPay = PAY;
		pGirlStudent->Pay(nMoneyToPay);
		m_nSum += nMoneyToPay;
		cout<<pGirlStudent->GetName()<<" is charged "<<nMoneyToPay<<endl;
	}
private:
	static const int PAY = 10;
	int m_nSum;
};

class StudentArray
{
public:
	StudentArray()
	{
		m_pStudentVec = new vector<Student*>();
		m_pVisitor = new ChargeMoney();
	}
	~StudentArray()
	{
		delete m_pStudentVec;
		delete m_pVisitor;
	}
	void Attach(Student *pStudent)
	{
		m_pStudentVec->push_back(pStudent);
	}
	void Detach(Student *pStudent)
	{
		vector<Student*>::iterator ite = m_pStudentVec->begin();
		while (ite != m_pStudentVec->end())
		{
			if ((*ite) == pStudent)
			{
				delete (*ite);
				ite = m_pStudentVec->erase(ite);
			}
			else
			{
				ite++;
			}
		}
	}

	void AllPayMoney()
	{
		vector<Student*>::iterator ite = m_pStudentVec->begin();
		while (ite != m_pStudentVec->end())
		{
			(*ite)->Accept(m_pVisitor);
			ite++;
		}
	}
private:
	vector<Student*> *m_pStudentVec;
	IVisitor *m_pVisitor;
};

int main() 
{ 
	/*Student *pS1 = new BoyStudent("John", 40);
	Student *pS2 = new GirlStudent("Lily", 50);

	StudentArray *pArr = new StudentArray();
	pArr->Attach(pS1);
	pArr->Attach(pS2);

	pArr->AllPayMoney();

	delete pArr;
	delete pS2;
	delete pS1;*/
	int a[2][2] = {0,1,2,3};
	int **p = (int **)a;
	cout<<p[3]<<endl;

	cin.get();
	return 0; 
}